package cn.tedu.travelsystem.preferance.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.base.response.StatusCode;
import cn.tedu.travelsystem.preferance.mapper.PreferenceMapper;
import cn.tedu.travelsystem.preferance.pojo.dto.PreferenceDTO;
import cn.tedu.travelsystem.preferance.pojo.entity.Preference;
import cn.tedu.travelsystem.preferance.pojo.vo.PreferenceVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/preferences")
@Api(tags = "05.用户偏好模块")
public class PreferenceController {

    @Autowired
    private PreferenceMapper preferenceMapper;

    @PostMapping
    @ApiOperation("添加用户偏好信息")
    @ApiOperationSupport(order = 10)
    public JsonResult addPreference(@Validated @RequestBody PreferenceDTO dto) {
        log.debug("PreferenceDTO = " + dto);
        Preference preference = new Preference();
        BeanUtils.copyProperties(dto, preference);
        int rows = preferenceMapper.insert(preference);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @DeleteMapping("/{prefId}")
    @ApiOperation("删除用户偏好信息")
    @ApiOperationSupport(order = 20)
    public JsonResult deletePreference(@PathVariable Integer prefId) {
        log.debug("prefId = " + prefId);
        int rows = preferenceMapper.deleteById(prefId);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @PutMapping("/{prefId}")
    @ApiOperation("更新用户偏好信息")
    @ApiOperationSupport(order = 30)
    public JsonResult updatePreference(@PathVariable Integer prefId, @Validated @RequestBody PreferenceDTO dto) {
        log.debug("prefId = " + prefId + ", PreferenceDTO = " + dto);
        Preference preference = new Preference();
        BeanUtils.copyProperties(dto, preference);
        preference.setPref_id(prefId);
        int rows = preferenceMapper.update(preference);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @GetMapping("/{prefId}")
    @ApiOperation("查询单个用户偏好信息")
    @ApiOperationSupport(order = 40)
    public JsonResult getPreference(@PathVariable Integer prefId) {
        log.debug("prefId = " + prefId);
        Preference preference = preferenceMapper.selectById(prefId);
        if (preference != null) {
            BeanUtils.copyProperties(preference,prefId);
            return JsonResult.ok(prefId);
        } else {
            return new JsonResult(StatusCode.DATA_NOT_FOUND);
        }
    }

    @GetMapping
    @ApiOperation("查询所有用户偏好信息")
    @ApiOperationSupport(order = 50)
    public JsonResult getAllPreferences() {
        log.debug("查询所有用户偏好信息");
        List<Preference> preferences = preferenceMapper.selectAll();
        List<PreferenceVO> vos = new ArrayList<>();
        for (Preference preference : preferences) {
            PreferenceVO vo = new PreferenceVO();
            BeanUtils.copyProperties(preference, vo);
            vos.add(vo);
        }
        return JsonResult.ok(vos);
    }

}